package com.icode.web.controller.house;

import com.icode.core.dto.house.AgreementDTO;
import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.model.house.EquipmentType;
import com.icode.core.model.house.HouseInfo;
import com.icode.core.shared.DataToJson;
import com.icode.core.shared.StringUtils;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-11-13
 * Time: 下午8:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/house/info")
public class HouseInfoController {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form/{buildingTypeGuid}", method = RequestMethod.GET)
    public ModelAndView showForm(@PathVariable String buildingTypeGuid, Model model) {
        List<EquipmentDTO> equipmentDTOs = houseService.getEquipmentDTOs(EquipmentType.EQUIPMENT);
        List<EquipmentDTO> supportingDTOs = houseService.getEquipmentDTOs(EquipmentType.SUPPORTING);
        HouseInfoDTO houseInfoDTO = houseService.findHouseInfoByBuildingTypeGuid(buildingTypeGuid);
        if (StringUtils.isEmpty(houseInfoDTO.getGuid())) {
            houseInfoDTO = new HouseInfoDTO(equipmentDTOs, supportingDTOs, buildingTypeGuid);
        } else {
            model.addAttribute("equipmentDTOs", houseInfoDTO.getEquipmentDTOs());
            model.addAttribute("supportingDTOs", houseInfoDTO.getSupportingDTOs());
            AgreementDTO agreementDTO = houseService.getAgreementDTOByHouseInfoDTO(houseInfoDTO);
            model.addAttribute("agreementDTO", agreementDTO);
            houseInfoDTO.setEquipmentDTOs(equipmentDTOs);
            houseInfoDTO.setSupportingDTOs(supportingDTOs);
        }
        return new ModelAndView("back/houseBase/houseInfo", "houseInfo", houseInfoDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String submitForm(HouseInfoDTO houseInfoDTO) {
        String guid = houseInfoDTO.getGuid();
        houseService.saveOrUpdateHouse(houseInfoDTO);
        return "redirect:/house/info/form/" + houseInfoDTO.getBuildingTypeGuid();
    }


    @RequestMapping(value = "updataimage", method = RequestMethod.POST)
    @ResponseBody
    public Object updataimage(MultipartFile filename) {
        String imageGuid = houseService.saveOrUpdateImage(filename);
        return imageGuid;
    }


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String showHouseBases(Model model) {
        List<HouseInfoDTO> houseInfoDTOs = houseService.getHouseInfoDTOs();
        model.addAttribute("list", houseInfoDTOs);
        return "back/houseBase/houseInfoOverView";
    }

    @RequestMapping(value = "update/{guid}")
    public ModelAndView update(@PathVariable String guid) {
        List<EquipmentDTO> equipmentDTOs = houseService.getEquipmentDTOs(EquipmentType.EQUIPMENT);
        List<EquipmentDTO> supportingDTOs = houseService.getEquipmentDTOs(EquipmentType.SUPPORTING);
        HouseInfoDTO houseInfoDTO = houseService.getHouseInfoByGuid(guid);
        houseInfoDTO.setEquipmentDTOs(equipmentDTOs);
        houseInfoDTO.setSupportingDTOs(supportingDTOs);
        return new ModelAndView("back/houseBase/houseInfo", "houseInfo", houseInfoDTO);
    }

    @RequestMapping(value = "delImg", method = RequestMethod.POST)
    public void deleteUploadImage(String imgGuid, HttpServletResponse response) {
        houseService.deleteUploadImageByGuidForHouseInfo(imgGuid);
        String content = "success";
        DataToJson.DataToJson(content, response);
    }

    @RequestMapping(value = "delHouseExtraFee", method = RequestMethod.POST)
    public void delHouseExtraFee(String guid, HttpServletResponse response) {
        if (!guid.isEmpty()) {
            houseService.deleteUploadHouseExtraFeeByGuid(guid);
        }
        String content = "success";
        DataToJson.DataToJson(content, response);
    }
}
